<?php

namespace App\Http\Controllers\Admin;

use App\Model\Campus;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class CampusController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $item = Campus::orderBy('id','desc')->paginate(15);
        return view('admin.campus.campus-list',compact('item'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.campus.campus-add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'title' => 'required',
            'des' => 'required'
        ],[
            'title.required'=>'请填写标题',
            'des.required'=>'请填写人物描述'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }

        if ($request->hasFile('file')){
            $file = $request->file('file');
            $fileName = ['png','jpg','jpeg','gif'];
            if (!in_array($file->getClientOriginalExtension(),$fileName)){
                return $this->error('不支持的图片格式');
            }
            $path = $request->file->store('uploads'.'/'.date('Y-m-d'));
        }else{
            return $this->error('请上传封面图片');
        }

        $m = new Campus();
        $m->name = $request->input('title');
        $m->des = $request->input('des');
        $m->content = $request->input('editorValue');
        $m->img_url = $path;
        if ($m->save()){
            return $this->success('发表成功');
        }else{
            return $this->error('发表失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Campus::where('id',$id)->first();
        return view('admin.campus.campus-edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(),[
            'title' => 'required',
            'des' => 'required'
        ],[
            'title.required'=>'请填写标题',
            'des.required'=>'请填写人物描述'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }

        if ($request->hasFile('file')){
            $file = $request->file('file');
            $fileName = ['png','jpg','jpeg','gif'];
            if (!in_array($file->getClientOriginalExtension(),$fileName)){
                return $this->error('不支持的图片格式');
            }
            $path = $file->store('uploads'.'/'.date('Y-m-d'));
        }else{
            $path = $request->input('img-url');
        }

        $m = Campus::find($id);
        $m->name = $request->input('title');
        $m->des = $request->input('des');
        $m->content = $request->input('editorValue');
        $m->img_url = $path;
        if ($m->save()){
            return $this->success('更改成功');
        }else{
            return $this->error('更改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Campus::where('id',$id)->delete();
        if ($data){
            return $this->success('删除成功');
        }else{
            return $this->success('删除失败');
        }
    }
}
